package org.gruposp2p.dnie.client.ui.dialogs;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.http.client.Request;
import com.google.gwt.http.client.RequestCallback;
import com.google.gwt.http.client.Response;
import com.google.gwt.user.client.ui.Button;
import com.google.gwt.user.client.ui.DecoratedTabPanel;
import com.google.gwt.user.client.ui.DialogBox;
import com.google.gwt.user.client.ui.FlexTable;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.HTMLTable.CellFormatter;
import com.google.gwt.user.client.ui.HorizontalPanel;
import org.gruposp2p.dnie.client.dto.DocumentToSignDTO;
import org.gruposp2p.dnie.client.event.DocumentToSignChangeEvent;
import org.gruposp2p.dnie.client.event.EventBusManager;
import org.gruposp2p.dnie.client.server.ServerFacade;
import org.gruposp2p.dnie.client.server.StatusData;
import org.gruposp2p.dnie.client.ui.panel.MainPanel;
import org.gruposp2p.dnie.client.ui.widget.GlassedDialog;
import org.gruposp2p.dnie.client.util.URLConstants;

public class DocumentToSignDetailsDialog extends DialogBox{

    DocumentToSignDTO documentToSign;

    public DocumentToSignDetailsDialog(DocumentToSignDTO document, String caption) {
        this.documentToSign = document;
        setText(caption);
        showDialog();
    }

    private void showDialog () {
        FlexTable layout = new FlexTable();
        CellFormatter cellFormatter = layout.getCellFormatter();
        layout.clear();
        layout.setCellSpacing(6);
        DecoratedTabPanel tabPanel = new DecoratedTabPanel();
        tabPanel.setWidth("400px");
        tabPanel.setAnimationEnabled(true);
        DocumentToSignDetailsDocumentPanel documentTab = 
        	new DocumentToSignDetailsDocumentPanel (documentToSign);
        HTML homeText = new HTML("Documento");
        tabPanel.add(documentTab, MainPanel.constants.DetailsDocumenTabCaption());
        // Add a tab with an image
        DocumentToSignDetailsStatisticsPanel statisticsPanel = 
        	new DocumentToSignDetailsStatisticsPanel(documentToSign);
        tabPanel.add(statisticsPanel, MainPanel.constants.StatisticDocumenTabCaption());
        // Return the content
        tabPanel.selectTab(0);
        tabPanel.ensureDebugId("DocumentToSignDetailsDialog");        
        layout.setWidget(0, 0, tabPanel);
        layout.setWidget(1, 0, getButtonPanel());
        cellFormatter.setHorizontalAlignment(1, 0, HorizontalPanel.ALIGN_RIGHT);
        setWidget(layout);
    }

    private HorizontalPanel getButtonPanel () {
        HorizontalPanel buttonPanel = new HorizontalPanel();
        buttonPanel.setSpacing(5);

        Button editButton = new Button(
            MainPanel.constants.editButtonText(),
            new ClickHandler() {
            public void onClick(ClickEvent event) {
                DocumentToSignDialog signatureDataDialog =
                    new DocumentToSignDialog(documentToSign);
                signatureDataDialog.show();
                signatureDataDialog.center();
                hide();
            }
            });

        Button closeButton = new Button(
            MainPanel.constants.closeButtonText(),
            new ClickHandler() {
            public void onClick(ClickEvent event) {
                hide();
            }
            });
        Button deleteButton = new Button(
            MainPanel.constants.deleteButtonText(),
            new ClickHandler() {
                public void onClick(ClickEvent event) {
                    ServerFacade.doDelete(
                            URLConstants.getDocumentToSignUrl(documentToSign.id),
                            new ServerRequestCallback());
                    hide();
                }
            });
        HTML separator = new HTML();
        separator.setWidth("100");
        //buttonPanel.add(deleteButton);
        buttonPanel.add(separator);
        //buttonPanel.add(editButton);
        buttonPanel.add(closeButton);
        //buttonPanel.setCellHorizontalAlignment(deleteButton, HorizontalPanel.ALIGN_LEFT);
        buttonPanel.setCellHorizontalAlignment(closeButton, HorizontalPanel.ALIGN_RIGHT);
        return buttonPanel;
    }

    private class ServerRequestCallback implements RequestCallback {

        @Override
        public void onError(Request request, Throwable exception) {
            StatusData status = new StatusData();
            status.description = exception.getMessage();
            GlassedDialog.getInstance().showErrorGettingResource(status);
        }

        @Override
        public void onResponseReceived(Request request, Response response) {
            if (response.getStatusCode() == Response.SC_OK) {
                DocumentToSignChangeEvent signatureDataChangeEvent =
                        new DocumentToSignChangeEvent(
                        documentToSign, DocumentToSignChangeEvent.DELETED);
                EventBusManager.getInstance().fireEvent(signatureDataChangeEvent);
            }
        }

    }
    
}